#include "cmd.h"

const Cmdinit cmdinit;


const string cmd::splitchar = cmdinit.splitchar;
const vector<string> cmd::cmdlist = cmdinit.cmdlist;


const unordered_set<string> logincmd::banedname = {"fuck","shit"};


void cmd::getcmdlineitems()
{    
    istringstream iss(cmdline);
    string token;
    while (iss >> token) {
        cmdlineitems.push_back(token);
    }
};


logincmd::logincmd(string cmdline)
{
    this->cmdline=cmdline;
    this->getcmdlineitems();
}

logoutcmd::logoutcmd(string cmdline)
{
    this->cmdline=cmdline;
    this->getcmdlineitems();

}


void logincmd::cmd2sql()
{
    
    this->table=string("users");
    this->key=string("(username,email)");
    this->value= string("('") + this->cmdlineitems[1]+string("','")+ this->cmdlineitems[2]+"')"; //string("'%s','%s'").;  
    this->wherekey=string("username");
    this->wherevalue=string("(username)");//string("('")+this->cmdlineitems[1]+string("')");

    //string res("logincmd sql string");
    //cout<<res<<endl;
    return ;
}

void logoutcmd::cmd2sql()
{

    string res("logoutcmd sql string");
    cout<<res<<endl;
    return ;
}


bool logincmd::isvalid()
{

    if(cmdlineitems.size()==0) {
        cout<<"please type some thing\n";
        return false;
    }
    else if(cmdlineitems.size()==1){
        cout<<"please type some parameter\n";
        return false;
    }else 
    {
        if(cmdlineitems[0]!="login")
        {
            cout<<"not a login cmd but call check\n";
            return false;
        }
        else if(banedname.find(cmdlineitems[1]) != banedname.end())
        {
            cout<<"your name is invalid\n";
            return false;
        }
    }
    
    return true;
}

bool logoutcmd::isvalid()
{
    if(cmdlineitems[0]!="logout")
    {
        cout<<"not a logout cmd but call check\n";
        return false;
    }else
    {
        cout<<"logout check";
        return true;
    }
}